package service

import (
	"context"
	"log"
	"time"

	"daxin.my/test-netserver/protocol/dubbo/model"

	hessian "github.com/apache/dubbo-go-hessian2"
	"github.com/apache/dubbo-go/config"
)

func init() {
	config.SetProviderService(new(DubboServiceProvider))
	// ------for hessian2------
	hessian.RegisterPOJO(&model.RequestData{})
	hessian.RegisterPOJO(&model.ResponseData{})
}

type DubboServiceProvider struct {
}

func (u *DubboServiceProvider) Request(ctx context.Context, reqData *model.RequestData) (*model.ResponseData, error) {
	log.Printf("Received Dubbo param: respbyte: %d, sleep: %dms\n", reqData.ResponseByte, reqData.Sleep)

	time.Sleep(time.Duration(reqData.Sleep) * time.Millisecond)

	message := make([]byte, reqData.ResponseByte)
	for i := 0; i < len(message); i++ {
		message[i] = 'a'
	}
	return &model.ResponseData{Message: string(message)}, nil
}

func (u *DubboServiceProvider) Reference() string {
	return "com.dubbo.demo.service.DubboService"
}
